"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : SQL_DDL_Drop_Owned
Case Name   : 修改数据库对象的属主，old_role和new_role无效值验证，合理报错
Create At   : 2022/08/25
Owner       : @li-xin12345
Description :
    1.创建用户
    2.old_role为不存在的用户的
    3.old_role参数包含特殊字符@*等
    4.old_role参数为空字符串
    5.old_role参数为纯数字
    6.old_role为初始用户
    7.新用户创建表
    8.old_role为不存在的用户的
    9.old_role参数包含特殊字符@*等
    10.old_role参数为空字符串
    11.old_role参数为纯数字
    12.删除表，删除用户
Expect      :
    1.创建成功
    2.合理报错
    3.合理报错
    4.合理报错
    5.合理报错
    6.合理报错
    7.创建成功
    8.合理报错
    9.合理报错
    10.合理报错
    11.合理报错
    12.删除成功
History     :
"""
import os
import unittest
from testcase.utils.Logger import Logger
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from yat.test import Node
from yat.test import macro


class DropOwned(unittest.TestCase):
    def setUp(self):
        self.logger = Logger()
        self.logger.info(f"-----{os.path.basename(__file__)} start-----")
        self.user_node = Node('PrimaryDbUser')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.user_name_1 = 'u_reassign_owned_0002_1'
        self.user_name_2 = 'u_reassign_owned_0002_2'
        self.user_special_characters = 'u@reassign*owned_0002'
        self.table_name = 't_reassign_owned_0002'

    def test_drop_owned(self):
        text = "-----step1:创建用户;expect:创建成功-----"
        self.logger.info(text)
        create_user = f'''drop user if exists {self.user_name_1};
                    create user {self.user_name_1} 
                    sysadmin password '{macro.COMMON_PASSWD}';'''
        create_msg = self.primary_sh.execut_db_sql(create_user)
        self.logger.info(create_msg)
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG,
                      create_msg, '执行失败' + text)
        self.assertIn(self.constant.CREATE_ROLE_SUCCESS_MSG,
                      create_msg, '执行失败' + text)

        text = "-----step2:old_role为不存在的用户;expect:合理报错-----"
        self.logger.info(text)
        sql_cmd = f"reassign owned by {self.user_name_2} to {self.user_name_1};"
        sql_msg = self.primary_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn(f'ERROR:  role "{self.user_name_2}" does not exist',
                      sql_msg, '执行失败' + text)

        text = "-----step3:old_role包含特殊字符@*等;expect:合理报错-----"
        self.logger.info(text)
        sql_cmd = f"reassign owned by {self.user_special_characters} " \
            f"to {self.user_name_1};" \
            f"reassign owned by &^%!@|* to {self.user_name_1};"
        sql_msg = self.primary_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertEquals(2, sql_msg.count(self.constant.SYNTAX_ERROR_MSG),
                          '执行失败' + text)

        text = "-----step4:old_role为空字符串;expect:合理报错-----"
        self.logger.info(text)
        sql_cmd = f"reassign owned by '' to {self.user_name_1};"
        sql_msg = self.primary_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn(self.constant.SYNTAX_ERROR_MSG, sql_msg, '执行失败' + text)

        text = "-----step5:old_role参数为纯数字，expect:合理报错-----"
        self.logger.info(text)
        sql_cmd = f"reassign owned by 0002 to {self.user_name_1};"
        sql_msg = self.primary_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn(self.constant.SYNTAX_ERROR_MSG, sql_msg, '执行失败' + text)

        text = "-----step6:old_role参数为初始用户，expect:合理报错-----"
        self.logger.info(text)
        sql_cmd = f"reassign owned by {self.user_node.ssh_user} " \
            f"to {self.user_name_1};"
        sql_msg = self.primary_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn(f'ERROR:  cannot reassign ownership of objects '
                      f'owned by role {self.user_node.ssh_user} '
                      f'because they are required by the database system',
                      sql_msg, '执行失败' + text)

        text = "-----step7:用户1创建表;expect:创建成功-----"
        self.logger.info(text)
        sql_cmd = f'''drop table if exists {self.table_name};
            create table public.{self.table_name}(id int, name varchar(15));
            insert into {self.table_name} values(1, '软件开发'),(2, '软件测试');
            \\d'''
        sql_msg = self.primary_sh.execut_db_sql(
            sql_cmd, f'-U {self.user_name_1} -W \'{macro.COMMON_PASSWD}\'')
        self.logger.info(sql_msg)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS,
                      sql_msg, '执行失败' + text)
        self.assertIn(self.constant.TABLE_CREATE_SUCCESS,
                      sql_msg, '执行失败' + text)
        self.assertIn(self.constant.INSERT_SUCCESS_MSG,
                      sql_msg, '执行失败' + text)
        self.assertGreater(len(sql_msg.splitlines()), 2, '执行失败' + text)
        self.assertIn(self.table_name, sql_msg.splitlines()[-2], '执行失败' + text)
        self.assertIn(self.user_name_1, sql_msg.splitlines()[-2], '执行失败' + text)

        text = "-----step8:new_role为不存在的用户;expect:合理报错-----"
        self.logger.info(text)
        sql_cmd = f"reassign owned by {self.user_name_1} to {self.user_name_2};"
        sql_msg = self.primary_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn(f'ERROR:  role "{self.user_name_2}" does not exist',
                      sql_msg, '执行失败' + text)

        text = "-----step9:new_role包含特殊字符@*等;expect:合理报错-----"
        self.logger.info(text)
        sql_cmd = f"reassign owned by {self.user_name_1} " \
            f"to {self.user_special_characters};" \
            f"reassign owned by {self.user_name_1} to &^%!@|*;"
        sql_msg = self.primary_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertEquals(2, sql_msg.count(self.constant.SYNTAX_ERROR_MSG),
                          '执行失败' + text)

        text = "-----step10:new_role为空字符串;expect:合理报错-----"
        self.logger.info(text)
        sql_cmd = f"reassign owned by {self.user_name_1} to '';"
        sql_msg = self.primary_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn(self.constant.SYNTAX_ERROR_MSG, sql_msg, '执行失败' + text)

        text = "-----step11:new_role参数为纯数字，expect:合理报错-----"
        self.logger.info(text)
        sql_cmd = f"reassign owned by {self.user_name_1} to 0002;"
        sql_msg = self.primary_sh.execut_db_sql(sql_cmd)
        self.logger.info(sql_msg)
        self.assertIn(self.constant.SYNTAX_ERROR_MSG, sql_msg, '执行失败' + text)

    def tearDown(self):
        text = "-----step12:删除表，删除用户，expect:删除成功-----"
        self.logger.info(text)
        drop_cmd = f"drop table {self.table_name};" \
            f"drop user {self.user_name_1} cascade;"
        drop_msg = self.primary_sh.execut_db_sql(drop_cmd)
        self.logger.info(drop_msg)
        self.assertIn(self.constant.TABLE_DROP_SUCCESS,
                      drop_msg, '执行失败' + text)
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG,
                      drop_msg, '执行失败' + text)
        self.logger.info(f"-----{os.path.basename(__file__)} end-----")
